package com.sy.controller;

import com.sy.dto.ExpRecord;
import com.sy.dto.JsonResult;
import com.sy.dto.LoginRecord;
import com.sy.redis.RedisClient;
import com.sy.service.PersonalService;
import com.sy.utils.BilibiliUtils;
import com.sy.utils.StateUtils;
import com.sy.vo.HeadPhotoVo;
import com.sy.vo.PersonalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

import static com.sy.constant.BusinessConstant.REDIS_EXP_PREFIX;
import static com.sy.constant.BusinessConstant.REDIS_RECORD_PREFIX;

/**
 * @author zbbstart
 * @date 2021/1/14 - 18:27
 */
@RestController
public class PersonalController {
    @Autowired
    private PersonalService personalService;
    @Autowired
    private RedisClient redisClient;


    /**
     * 更新个人资料
     * @param personalVo  封装的参数
     * @param res         响应
     * @return            JSON格式
     */
    @RequestMapping(value = "persons", method = RequestMethod.PUT )
    public JsonResult updatePersonalInfo(@RequestBody PersonalVo personalVo, HttpServletResponse res){
        JsonResult result = new JsonResult();
        System.out.println(personalVo);
        try {
            boolean flag = personalService.updatePersonalInfo(personalVo);
            if (flag){
                result.setCode(200);
                result.setData("更新成功...");
            }else {
                result.setCode(400);
                result.setErrMsg("更新失败...");
                res.setStatus(result.getCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = StateUtils.is500(res);
        }
        return result;

    }

    /**
     * 用户登录记录
     * @param req
     * @param res
     * @return
     */
    @RequestMapping(value = "persons", method = RequestMethod.GET, params = "userName")
    public JsonResult updateLoginRecord(HttpServletRequest req, HttpServletResponse res){
        JsonResult result = new JsonResult();
        try {
            String userName = req.getParameter("userName");
            String loginRecordKey = REDIS_RECORD_PREFIX + userName;
            if (redisClient.exists(loginRecordKey)){
                List<String> list = redisClient.lrange(loginRecordKey, (long) 0, redisClient.llen(loginRecordKey));
                List newlist = BilibiliUtils.toList(list, LoginRecord.class);
                result.setCode(200);
                result.setData(newlist);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = StateUtils.is500(res);
        }
        return result;
    }

    /**
     * 登录经验奖励
     * @param req
     * @param res
     * @return
     */
    @RequestMapping(value = "persons", method = RequestMethod.GET, params = "user")
    public JsonResult updateExpRecord(HttpServletRequest req, HttpServletResponse res){
        JsonResult result = new JsonResult();
        try {
            String user = req.getParameter("user");
            String expKey = REDIS_EXP_PREFIX + user;
            if (redisClient.exists(expKey)){
                List<String> list = redisClient.lrange(expKey,  0L, redisClient.llen(expKey));
                List newlist = BilibiliUtils.toList(list, ExpRecord.class);
                result.setCode(200);
                result.setData(newlist);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = StateUtils.is500(res);
        }
        return result;
    }

    /**
     * 更改头像
     * @return
     */
    @RequestMapping(value = "photos", method = RequestMethod.PUT)
    public JsonResult updateHeadPhoto(@RequestBody HeadPhotoVo headPhotoVo, HttpServletResponse res){
        JsonResult result = new JsonResult();
        try {
            personalService.updateHeadPhoto(headPhotoVo.getHeadphoto(),headPhotoVo.getUserName());
            result.setCode(200);
            result.setData("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = StateUtils.is500(res);
        }
        return result;
    }
}
